#!/bin/bash
cd "$(dirname "$0")"
function usage() {
  echo >&2 "Usage: $0 name address [name address ...]"
  exit 2
}

function find_single() {
  name=$1
  address=$2
  addr_last12=`echo -n "$address" | tail -c 3`
  grep -i -e "^$name .*$addr_last12$" db/*.symbols \
    | perl -n -e '/db\/(.*)\.symbols/ && print "$1\n"' \
    | sort
}

function find() {
  [[ $# -lt 2 ]] && usage
  name=$1; shift
  address=$1; shift
  if [[ $# == 0 ]]; then
    find_single $name $address
  else
    comm -12 \
      <(find_single $name $address) \
      <(find "$@")
  fi
}

ret=1
for id in `find "$@"`; do
  echo "`cat db/${id}.info` ($id)"
  ret=0
done
exit $ret
